package com.bnz.service.impl;

import com.bnz.dao.ClassesDao;
import com.bnz.dao.StudentDao;
import com.bnz.pojo.Classes;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassesDao classesDao;

    //查询所有学生
    @Override
    public List<Student> findAll() {
        //查询所有学生
        List<Student> students = studentDao.findAll();
        //遍历学生，关联对应的班级名称
        for (Student student :students) {
            //根据班级id得到班级名称
            Optional<Classes> byId = classesDao.findById(student.getCid());
            //
            Classes classes = byId.get();
            //将班级名称与学生中的班级id关联
            student.setCname(classes.getCname());
        }
        //返回结果
        return students;
    }
}
